package cn.xfdzx.android.apps.shop.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;
import android.widget.TextView;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.demo.common.utils.TCConstants;

/**
 * @autor: zf
 * @Description: 消息弹窗
 * @Date: 2020/7/6 12:18
 */
public class CustomerPopwindow extends PopupWindow{
    private Activity mContext;
    private View mView;
    private View mRootView;
    private TextView tv_context;
    public CustomerPopwindow(Activity cx) {
        this.mContext = cx;
        mView = LayoutInflater.from(mContext).inflate(R.layout.pw_customer, null);
        setContentView(mView);
        mRootView = mView.findViewById(R.id.sv_popup_root);
        tv_context = mView.findViewById(R.id.tv_context);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closePopupWindow();
            }
        });
    }
    public void showPopupWindow(int msgType,String msgContext) {
        //initAnims();
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.LEFT, 20, 30);
      //  mRootView.setAnimation(animShow);
        if (msgType == TCConstants.MEMBER_ENTER){
            tv_context.setText(msgContext+"来了~");
        }

    }

    public void closePopupWindow() {
        animHide.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                CustomerPopwindow.this.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mRootView.clearAnimation();
        mRootView.setAnimation(animHide);
    }

    public View getRootView() {
        return mView;
    }

    /**
     * 显示时的动画
     */
    private Animation animShow;
    /**
     * 隐藏时的动画
     */
    private Animation animHide;

    private void initAnims() {
        animShow = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, -100,
                Animation.RELATIVE_TO_SELF, 20,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0);
        animShow.setDuration(10);

        animHide = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 20,
                Animation.RELATIVE_TO_SELF, -100,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0);
        animHide.setDuration(10);
    }
}
